// SpringSet.cpp: implementation of the SpringSet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SpringSet.h"
#include "ProductCatalog.h"
#include "ProductCatalogManager.h"
#include "StandardOfSpring.h"
#include "PhsShare.h"
#include "PersistentFacade.h"
#include "StandardGenerator.h"
#include "PartSort.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace JointSystem;
using namespace DAWrapper;

SpringSet::SpringSet()
{
	
}

SpringSet::~SpringSet()
{
}

BOOL SpringSet::GetSpringSet(const CString& strRegular)
{
	CString strSql;

	Close();
	if(!GetSpringTableName())
		return FALSE;

	strSql.Format(_T("select * from %s where %s"), m_strTableName, strRegular);

	try
	{
		
		CString strDataSource;
		strDataSource = JointSystem::ProductCatalogManager::Instance()->GetProductCatalog(this->GetDataStandard())->DataSourceURI();
		_ConnectionPtr pcon = *DBConnectionManager::Instance()->GetConnectionPtrByUri(strDataSource);
		m_rs = pcon->Execute(_bstr_t(strSql), NULL, adCmdText);
	}
	CATCH_COM_ERROR(m_rs, FALSE)

	return TRUE;
}

Spring* SpringSet::GetFirstSpring()
{
	try
	{
		if(m_rs == NULL || (m_rs->adoEOF && m_rs->BOF))
			return NULL;
		
		m_rs->MoveFirst();
		return GetSpring();
	}
	CATCH_COM_ERROR(m_rs, NULL)
}

Spring* SpringSet::GetNextSpring()
{
	try
	{
		m_rs->MoveNext();
		if(m_rs == NULL || m_rs->adoEOF || m_rs->BOF)
		{		
			Close();
			return NULL;
		}
		
		return GetSpring();
	}
	CATCH_COM_ERROR(m_rs, NULL)
}

BOOL SpringSet::GetSpringTableName()
{
	ProductCatalog* pCatalog = ProductCatalogManager::Instance()->GetProductCatalog(this->GetDataStandard());
	
	Standard* pStandard = GetPersistentFacade().GetStandardGenerator()->Generate(pCatalog);
	ASSERT(pStandard->GetStyle() == eSPRING);

	this->m_strTableName = static_cast<StandardOfSpring*>(pStandard)->GetPropertyDataTable();
	this->m_theTables.Add(m_strTableName);
	if(this->m_strTableName.IsEmpty())
		return FALSE;
	return TRUE;
}

Spring* SpringSet::GetSpring()
{
	Spring* pSpring = new Spring;
	if(pSpring == NULL)
	{
		ReportMemoryError();
		return NULL;
	}

	pSpring->PreReduceValue(vtof(m_rs->GetCollect(_T("PreCmprs"))));
	pSpring->Journey(vtof(m_rs->GetCollect(_T("HopMax"))));
	pSpring->MinPress(vtof(m_rs->GetCollect(_T("pgzmin"))));
	pSpring->MaxPress(vtof(m_rs->GetCollect(_T("pgzmax"))));
	pSpring->Number(vtoi(m_rs->GetCollect(_T("SEQ"))));

	return pSpring;
}